﻿export function render_list(list,fn) {
	var vnode=[];
	try{
		if(list.length<1){
			return vnode;
		}
		list.forEach((item,index)=>{
			console.log(fn(item,index));
			vnode.push(fn(item,index));
		});
	}catch(e){
	}
	return vnode;
}
export function render_obj(obj,fn){
	var piece=[];
	for(let prop in obj){
		piece.push(fn(prop,obj[prop]));
	}
	return piece;
}
export function getNodeByPath(nodeTree,path){
	var tem=nodeTree;
	for(let i in path){
		if(i==0){
			tem=tem[path[i]];
		}else{
			tem=tem.children[path[i]]
		}
	}
	for(let i=0;i<path.lenth;i++){
	}
	return tem;
}
export function sizeToNum(sizeStr){
	var sizeStr= sizeStr.replace('px','');
	return parseFloat(sizeStr);
}
export function strToNum(str,num){
	var num=num ? num:0;
	var value= parseFloat(str);
	return parseFloat(value.toFixed(num))
}
export function menuRender(data,fields,fn){
	var defaultFields={
		id:"id",
		pId:"pId"
	}
	var fields=Object.assign({},defaultFields,fields);
	console.log(fields);
	var path={},
	/*筛选出一级节点*/
	findAllroot=function(nodeAll,vNodeGroup){
		console.log(vNodeGroup);
		var rootNodes=[]
		var test=true //true为一级节点
		for(let key in vNodeGroup){
			for(let i=0;i<nodeAll.length;i++){
				if(key=="key_"+nodeAll[i].id){
					test=false
					break;
				}
			}
			console.log(test);
			if(test){
				var _group={}
				_group[key]=vNodeGroup[key]
				console.log(_group);
				rootNodes.push(_group)
			}
		}
		console.log(rootNodes);
		return rootNodes
	},
	/*渲染节点为虚拟DOM*/
	vNodeRender=function(dataList,fn,vNodeGroup,path){
		var menuList=[],
		node;
		for(let index=0;index<dataList.length;index++){
			var item=dataList[index];
			if(path.length==0){
				path["key_"+dataList[index].id]=[index];
				if(path){
					dataList[index].level=path.length+1;
				}
			}else{
				dataList[index].isLeaf=!hasChild(dataList[index],vNodeGroup);
				if(path){
					dataList[index].level=path.length+1;
				}
				if(path){
					/*path.forEach((item,index)=>{
						this.path["key_"+dataList[i].id].push(item)
					})*/
					path["key_"+dataList[index].id]=path.concat(index);
				}
			}
			node=fn(dataList[index]);
			menuList.push(node);
		}
		return menuList;
	},
	/*把节点以parentId分组*/
	getNodeGroup=function(nodeList){
		var group={};
		console.log(nodeList);
		for(let i=0;i<nodeList.length;i++){
			group["key_"+nodeList[i][fields.pId]]=[];
		}
		for(let i=0;i<nodeList.length;i++){
			group["key_"+nodeList[i][fields.pId]].push(nodeList[i]);
		}
		return group;
	},
	getByPath=function(vNode,path){
		var tem=vNode;
		path.forEach(function(item,index){
			if(index==0){
				tem=tem[item];
			}else{
				tem=tem.props.children[item];
			}
		});
		return tem;
	},
	hasChild=function(node,vNodeGroup){
		if(vNodeGroup){
			return vNodeGroup.hasOwnProperty("key_"+node.id);
		}else{
			return false;
		}
	},
	main=function(data,fn){
		var _this=this;
		var rootList=[],
		vNode=[],
		vNodeGroup={};
		vNodeGroup=getNodeGroup(data);
		//获取一级节点
		rootList=findAllroot(data,vNodeGroup);
		console.log(rootList);
		//挂载一级级节点
		for(let prop in rootList){
			var arr_tem= vNodeRender(rootList[prop],fn,vNodeGroup,path[prop])
			vNode=vNode.concat(arr_tem)
		}
		//删除一级节点，因为一级节点已经进入虚拟DOM树了
		for(let prop in rootList){
			delete rootList[prop]
		}
		var usedNode=[];
		/*第一层循环用于保证分组列表里面的每一组都在整个的节点集合中寻找它的父级，
		循环退出的条件是，分组列表里的每一项在当前的路径记录都没有对应的父节点记录*/
		var times=0;
		do{
			usedNode=[];
			for(let prop in vNodeGroup){
				//如果
				if(path.hasOwnProperty(prop)){
					var children =vNodeRender(vNodeGroup[prop],fn,vNodeGroup,path[prop]);
					var targetNode=getByPath(vNode,path[prop]);
					usedNode.push(prop);
					if(targetNode){
						targetNode.props.children=children;
					}
				}
			}
			usedNode.forEach(function(item){
				delete vNodeGroup[item];
			})
			times++;
		}while(usedNode.legnth>0)
		for(let prop in vNodeGroup){
			var children2 =vNodeRender(vNodeGroup[prop],fn,vNodeGroup,[]);
			vNode.push(children2);
		}
		return vNode;
	};
	return main(data,fn)
}
